<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Channel extends MY_Controller {
    
    private $model='channel';
    private $table='channel';

	function __construct()
    {
        parent::__construct();
		$this->check_manage_session();//判断用户是否登录
    }
	
	public function index()
	{
		$data['list'] = $this->admin_model->get_list($this->table,'','','','sort ASC,id DESC');
	    $data['sidelist'] = $this->admin_model->get_list($this->table,'','','','sort ASC,id DESC');
		$this->load->view($this -> config->item('system_manage_path').'channel_list',$data);
	}
	
	public function add()
	{
	    $data['show'] = $this->admin_model->get_select_max($this->table);
	    $data['sidelist'] = $this->admin_model->get_list($this->table,'','','','sort ASC,id DESC');
	    $this->load->view($this -> config->item('system_manage_path').'channel_add',$data);
	}
	
	public function edit($id){
		$data['show'] = $this->admin_model->get_show($this->table,$id);
	    $data['sidelist'] = $this->admin_model->get_list($this->table,'','','','sort ASC,id DESC');
		$this->load->view($this -> config->item('system_manage_path').'channel_edit',$data);
	}
	
	public function save($action,$id=NULL){
	    $this->load->helper('file');
		$post=$this->input->post();
		$data=unset_array($post,'lcomeurl'); //过滤无效表单项
		
		if($action=='add' or $action=='edit'){
    		if(empty($post['name'])){
    			$this->error('请输入模块名称',2);
    		}
    		if(empty($post['sub_name'])){
    		    $this->error('请输入模块简称',2);
    		}
    		if(empty($post['en_name'])){
    		    $this->error('请输入模块英文',2);
    		}
    		if(empty($post['sort'])){
    		    $this->error('请输入模块排序',2);
    		}
		}

		$en_name=$this->input->post('en_name');
		$lcomeurl=get_comurl($this->input->post('lcomeurl'));
		
		if($action=='add'){
		    $action_text="添加";
		    $data['createtime'] = time();
		    
		    $string=array(
		        'id' => array(
		            'type' => 'INT',
		            'constraint' => 11,
		            'null' => TRUE,
		            'unsigned' => TRUE,
		            'auto_increment' => TRUE
		        ),
		        'cat_id' => array(
		            'type' => 'INT',
		            'constraint' => 11,
		        ),
		        'title' => array(
		            'type' =>'VARCHAR',
		            'constraint' => '100',
		        ),
		        'keywords' => array(
		            'type' =>'VARCHAR',
		            'constraint' => '100',
		        ),
		        'description' => array(
		            'type' =>'VARCHAR',
		            'constraint' => '100',
		        ),
		        'pic' => array(
		            'type' =>'VARCHAR',
		            'constraint' => '100',
		        ),
		        'content' => array(
		            'type' => 'TEXT',
		        ),
		        'hits' => array(
		            'type' => 'INT',
		            'constraint' => 11,
		        ),
		        'status' => array(
		            'type' => 'INT',
		            'constraint' => 1,
		        ),
		        'sort' => array(
		            'type' => 'INT',
		            'constraint' => 11,
		        ),
		        'createtime' => array(
		            'type' => 'INT',
		            'constraint' => 11,
		        ),
		    );
		    
		    $file_string=read_file(APPPATH.'controllers/admin/article.php');
		    $result=write_file(APPPATH.'controllers/admin/'.$en_name.'.php', $file_string);
		    $file_string=read_file(APPPATH.'views/admin/article_list.php');
		    $result=write_file(APPPATH.'views/admin/'.$en_name.'_list.php', $file_string);
		    $file_string=read_file(APPPATH.'views/admin/article_add.php');
		    $result=write_file(APPPATH.'views/admin/'.$en_name.'_add.php', $file_string);
		    $file_string=read_file(APPPATH.'views/admin/article_edit.php');
		    $result=write_file(APPPATH.'views/admin/'.$en_name.'_edit.php', $file_string);
		    
		    if ($result) {
		      $result=$this->admin_model->create($en_name,$string);
		    }
			if ($result){
			    $result=$this->admin_model->add($this->table,$data);
			}
		}elseif($action=='edit'){
		    $action_text="修改";
		    if(empty($id)){
		        $this->error('参数错误',2);
		    }
			$result=$this->admin_model->edit($this->table,$data,$id);
		}elseif($action=='del'){
		    $action_text="删除";
		    if(empty($id)){
		        $this->error('参数错误',2);
		    }
			$result=$this->admin_model->del($this->table,$id);
		}
		
		if($result){
		    $lcomeurl=str_replace("welcome", "channel", $lcomeurl);
			$this->success($action_text.'成功',2,$lcomeurl);
		}else{
			$this->error($action_text.'失败',2);
		}
	}
	
}
